package com.leadbank.stormstudy.kafka;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * Created by hp on 2018/7/13.
 */
public class JedisUtil {

    private static Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");

    public static JedisCluster getJedis(String address){
        JedisCluster jedisCluster = null;
        try {
            Set<HostAndPort> haps = parseHostAndPort(address);
            jedisCluster = new JedisCluster(haps, 3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jedisCluster;
    }

    private static Set<HostAndPort> parseHostAndPort(String address) throws Exception {
        try {
            String[] addressStr = address.split("\\|\\|\\|");
            Set<HostAndPort> haps = new HashSet();
            String[] arr$ = addressStr;
            int len$ = addressStr.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String val = arr$[i$];
                boolean isIpPort = p.matcher(val).matches();
                if(!isIpPort) {
                    throw new IllegalArgumentException("ip 或 port 不合法");
                }

                String[] ipAndPort = val.split(":");
                HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));
                haps.add(hap);
            }

            return haps;
        } catch (IllegalArgumentException var10) {
            throw var10;
        } catch (Exception var11) {
            throw new Exception("解析 jedis 配置文件失败", var11);
        }
    }
}
